package com.news.vo;


import com.news.pojo.User;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * 在线用户实体类
 */
public class LoginUser implements HttpSessionBindingListener, HttpSessionAttributeListener, Serializable {
    private static final long serialVersionUID = 1L;
    private String ip;
    private String type;//手机、平板、pc
    private User user;

    public LoginUser() {
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    /**
     * 将对象放入session中
     * @param httpSessionBindingEvent
     */
    @Override
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
        ServletContext application = httpSessionBindingEvent.getSession().getServletContext();
        HashMap<String,LoginUser> map = (HashMap<String,LoginUser>)application.getAttribute("loginUserMap");
        if(map == null){map = new HashMap<String,LoginUser>();}
        map.put(user.getUid()+"",this);
        //遍历map
        System.out.println("~~~~~~~~~~~~~~~~~~~在线用户");
        Set<String> keys = map.keySet();
        Iterator<String> it = keys.iterator();
        while(it.hasNext()){
            String key = it.next();
            LoginUser loginUser = map.get(key);
            System.out.println(key+","+loginUser.getType()+","+loginUser.getUser().getUname()+","+loginUser.getUser().getUrole());
        }
    }

    /**
     * 将对象从session解绑
     * @param httpSessionBindingEvent
     */
    @Override
    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
        ServletContext application = httpSessionBindingEvent.getSession().getServletContext();
        HashMap<String,LoginUser> map = (HashMap<String,LoginUser>)application.getAttribute("loginUserMap");
        if(map == null){map = new HashMap<String ,LoginUser>();}
        map.remove(this.getUser().getUid());
        //遍历map
        System.out.println("~~~~~~~~~~~~~~~~~在线用户");
        Set<String> keys = map.keySet();
        Iterator<String> it = keys.iterator();
        while(it.hasNext()){
            String key = it.next();
            LoginUser loginUser = map.get(key);
            System.out.println(key+","+loginUser.getType()+","+loginUser.getUser().getUname()+","+loginUser.getUser().getUrole());
        }
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}
